1223C - Save the Nature - CodeForces Solution


binary search greedy *1600

Please click on ads to support us..

Python Code:

import math
R=lambda:[*map(int,input().split())]
f=lambda m:p[m//b]*(y-x)+(p[m//c]+p[m//b+m//a-m//c])*x<100*k
q,=R()
for _ in[0]*q:
 l=R()+[0];p=[0]+sorted(R())[::-1]
 for i in range(l[0]):p[i+1]+=p[i]
 (x,a),(y,b)=sorted((R(),R()));c=a*b//math.gcd(a,b);k,=R()
 while l[0]-l[1]>1:m=sum(l)//2;l[f(m)]=m
 print((l[0],-1)[f(l[0])])

C++ Code:

#include<bits/stdc++.h>
using namespace std;

typedef int64_t ll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<int64_t> vll;

#define pb push_back
#define pob pop_back()
#define mp make_pair
#define sz size()
#define ff first
#define ss second
#define PI 3.14159265359
#define M1 ll(998244353)
#define M2 ll(1000000007)
#define pres(x) cout<<fixed; cout<<setprecision(x);
#define out(x) cout<<x<<endl;

ll gcd(ll a, ll b) {
    if (b > a) {return gcd(b, a);}
    if (b == 0) {return a;}
    return gcd(b, a % b);
}
int power(int a,int b){
    int ans=1;
    while(b!=0){
        if(b&1){ans=(ans*a)%M2;b--;}
        a=(a*a)%M2;b/=2;
    }
    return ans%M2;
}
ll expo(ll a, ll b, ll mod) {
    ll res = 1; 
    while (b > 0) {
        if (b & 1) res = (res * a) % mod;
        a = (a * a) % mod; 
        b = b >> 1;
    } 
    return res;
}
int invmod(int n){
    int ans=1;
    ans=power(n,M2-2);
    return ans;
}
int lcm(int a,int b){
    int g=gcd(a,b);
    a/=g;a*=b;
    return a;
}

bool find(int num,vll &v,int x,int a,int y,int b,ll k,int lc){
    // int comm=num/lc;
    // int frst=(num/a)-comm;
    // int sec=(num/b)-comm;
    int comm=0,frst=0,sec=0;
    for(int i=1;i<=num;i++){
      if(i%a==0 && i%b==0) comm++;
      else if(i%a==0) frst++;
      else if(i%b==0) sec++;
    }
    int i=0;
    ll amnt=0;
    while(i<comm) amnt+=(x+y)*v[i++];
    while(i<(comm+frst)) amnt+=x*v[i++];
    while(i<(comm+frst+sec)) amnt+=y*v[i++];
    return amnt>=k;
}

void solve(){
    int n;
    cin>>n;
    vll v(n);
    for(int i=0;i<n;i++) {cin>>v[i];v[i]/=100;}
    sort(v.rbegin(),v.rend());
    int x,a;
    cin>>x>>a;
    int y,b;
    cin>>y>>b;
    if(x<y) {swap(x,y);swap(a,b);}
    ll k;
    cin>>k;
    int gc=__gcd(a,b);
    int lc=(a*1ll*b)/gc;
    int l=1,r=n;
    while(r-l>1){
        int mid=(l+r)/2;
        if(find(mid,v,x,a,y,b,k,lc)) r=mid;
        else l=mid+1;
    }
    if(find(l,v,x,a,y,b,k,lc)){
        out(l)
        return;
    }
    if(find(r,v,x,a,y,b,k,lc)){
        out(r)
        return;
    }
    // if(r>n) r=-1;
    out(-1)
    return;
}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    ll t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut
1101A - Minimum Integer
985D - Sand Fortress
1279A - New Year Garland
1279B - Verse For Santa
202A - LLPS
978A - Remove Duplicates
1304A - Two Rabbits
225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks